********************************************************************************
*Political approval ratings and economic performance: evidence from Latin America
*Rodrigo Cerda and Rodrigo Vergara
*
********************************************************************************

clear all
set more off

global path "C:\Users\robca\OneDrive\Escritorio\CEP\Papers\Papers 2020\Paper - Cerda, Gallardo, Vergara\Actualización de paper\Archivos para enviar a revista"

*insert your path here

cd "$path"
use "data"


*Excluding Venezuela

drop if country=="Venezuela"

*Generate variables

encode country, g(country2)
g lgdp=log(gdp_pc_ppp)
label var lgdp "Log (gdp per capita, ppp)"
g lgdp2=lgdp*lgdp


*Declare data to be panel data
tsset country2 year
sort country year


//-------
//Table 1
//-------

putexcel set "Table 1", replace
putexcel A1=""
putexcel B1="Observations"
putexcel C1="Mean"
putexcel D1="Standard deviation"
putexcel E1="Minimum"
putexcel F1="Max"

sum gob_aprob
putexcel A2="Complete Sample"
putexcel B2=matrix(r(N))
putexcel C2=matrix(r(mean))
putexcel D2=matrix(r(sd))
putexcel E2=matrix(r(min))
putexcel F2=matrix(r(max))

local i=3
foreach c in Argentina Bolivia Brazil Chile Colombia "Costa Rica" "Dominican Republic" Ecuador "El Salvador" Guatemala Honduras Mexico Nicaragua Panama Paraguay Peru Uruguay {
	sum gob_aprob if country=="`c'"
	putexcel A`i'="`c'"
	putexcel B`i'=matrix(r(N))
	putexcel C`i'=matrix(r(mean))
	putexcel D`i'=matrix(r(sd))
	putexcel E`i'=matrix(r(min))
	putexcel F`i'=matrix(r(max))
	
	local ++i
}

//-----------
//Regressions
//-----------

*Table 2

qui xtreg gob_aprob i.year  c_gpub_growth , fe robust
outreg2 using Table2, lab word replace  keep(c_gpub_growth) sortvar(c_gpub_growth ) dec(3) ///
addtext(Year Effect, Yes, Country Effect, Yes )  
qui xtreg gob_aprob i.year c_gpub_growth emp_wb_ilo inf_av gdp_crec_cc , fe robust
outreg2 using Table2, lab word append  keep(c_gpub_growth emp_wb_ilo inf_av gdp_crec_cc) dec(3) ///
addtext(Year Effect, Yes, Country Effect, Yes )  
qui xtreg gob_aprob i.year c_gpub_growth emp_wb_ilo inf_av gdp_crec_cc inauguration_year  t_vic, fe robust
outreg2 using Table2, lab word append  keep(c_gpub_growth emp_wb_ilo inf_av gdp_crec_cc inauguration_year  t_vic) dec(3) ///
addtext(Year Effect, Yes, Country Effect, Yes )  
qui xtreg gob_aprob i.year c_gpubt_pib emp_wb_ilo inf_av gdp_crec_cc inauguration_year  t_vic, fe robust
outreg2 using Table2, lab word append  keep(c_gpubt_pib emp_wb_ilo inf_av gdp_crec_cc inauguration_year  t_vic) dec(3) ///
addtext(Year Effect, Yes, Country Effect, Yes )  


*Table 3

qui xtreg gob_aprob i.year  c_gpub_nosoc_growth c_gsoc_growth , fe robust
outreg2 using Table3, lab word replace  keep(c_gpub_nosoc_growth c_gsoc_growth)  dec(3) ///
addtext(Year Effect, Yes, Country Effect, Yes )  
qui xtreg gob_aprob i.year c_gpub_nosoc_growth c_gsoc_growth emp_wb_ilo inf_av gdp_crec_cc , fe robust
outreg2 using Table3.doc, lab word append  keep(c_gpub_nosoc_growth c_gsoc_growth emp_wb_ilo inf_av gdp_crec_cc )  dec(3)  ///
addtext(Year Effect, Yes, Country Effect, Yes )  
qui xtreg gob_aprob i.year c_gpub_nosoc_growth c_gsoc_growth emp_wb_ilo inf_av gdp_crec_cc inauguration_year  t_vic, fe robust
outreg2 using Table3.doc, lab word append  keep(c_gpub_nosoc_growth c_gsoc_growth emp_wb_ilo inf_av gdp_crec_cc inauguration_year  t_vic)  dec(3) ///
addtext(Year Effect, Yes, Country Effect, Yes )  


*Table 4 y 5

xtivreg gob_aprob  i.year  emp_wb_ilo inf_av gdp_crec_cc  t_vic inauguration_year (c_gpub_growth =i.dsigno homeless  insured_damages total_damages lgdp lgdp2), first vce(robust) fe
outreg2 using Table5.doc, lab keep(emp_wb_ilo inf_av gdp_crec_cc  t_vic inauguration_year c_gpub_growth) cttop(Segond Stage) addtext(Year Effect, Yes, Country Effect, Yes )   dec(3)   replace
xtivreg gob_aprob  i.year  emp_wb_ilo inf_av gdp_crec_cc  t_vic inauguration_year (c_gpub_nosoc_growth c_gsoc_growth =i.dsigno homeless  insured_damages total_damages  lgdp lgdp2), first vce(robust) fe
outreg2 using Table5.doc, lab keep(emp_wb_ilo inf_av gdp_crec_cc  t_vic inauguration_year c_gpub_nosoc_growth c_gsoc_growth) cttop(Second Stage) addtext(Year Effect, Yes, Country Effect, Yes )   dec(3)  append

xtivreg gob_aprob  i.year  emp_wb_ilo inf_av gdp_crec_cc  t_vic inauguration_year (c_gpub_growth =i.dsigno homeless lgdp lgdp2 insured_damages total_damages), first vce(robust) fe
xtreg c_gpub_growth i.dsigno homeless  insured_damages total_damages lgdp lgdp2 i.year  emp_wb_ilo inf_av gdp_crec_cc  t_vic inauguration_year if e(sample)==1, vce(robust) fe
outreg2 using Table4.doc, lab keep(i.dsigno homeless lgdp lgdp2 insured_damages total_damages) cttop(First Stage) addtext(Year Effect, Yes, Country Effect, Yes )   dec(4)   replace
xtivreg gob_aprob  i.year  emp_wb_ilo inf_av gdp_crec_cc  t_vic inauguration_year (c_gpub_nosoc_growth c_gsoc_growth =i.dsigno homeless lgdp lgdp2 insured_damages total_damages), first vce(robust) fe
xtreg c_gpub_nosoc_growth i.dsigno homeless  insured_damages total_damages lgdp lgdp2 i.year  emp_wb_ilo inf_av gdp_crec_cc  t_vic inauguration_year if e(sample)==1, vce(robust) fe
outreg2 using Table4.doc, lab keep(i.dsigno homeless lgdp lgdp2 insured_damages total_damages) cttop(First Stage) addtext(Year Effect, Yes, Country Effect, Yes )   dec(4)   append
xtreg c_gsoc_growth i.dsigno homeless  insured_damages total_damages lgdp lgdp2 i.year  emp_wb_ilo inf_av gdp_crec_cc  t_vic inauguration_year if e(sample)==1, vce(robust) fe
outreg2 using Table4.doc, lab keep(i.dsigno homeless lgdp lgdp2 insured_damages total_damages) cttop(First Stage) addtext(Year Effect, Yes, Country Effect, Yes )   dec(4)   append

//--------
//Figures
//--------

preserve
collapse  gob_aprob gdp_crec_cc c_ti_fob c_gtot_hab c_gtot_pib, by(year)
merge 1:1 year using "Comodity_Index_data"
label var gob_aprob "Gov. approval, %"
label var year "Year"
label var gdp_crec_cc "Growth rate, %"
label var c_ti_fob "Terms-of-trade index (goods, FOB), 2010=100"
label var BCOMIndex "Com. index (total), 2002-18=100"
label var BCOMENIndex "Com. index energy, 2002-18=100"
label var BCOMGRIndex "Com. index grains, 2002-18=100"
label var BCOMINIndex "Com. index industrial metals, 2002-18=100"
label var c_gtot_hab "Per capita social spending, US$ 2010"

*Figure 1
tsline gob_aprob

*Figure 2
twoway tsline gob_aprob , yaxis(2) || tsline gdp_crec_cc , yaxis(1)

*Figure 3
twoway tsline gob_aprob, yaxis(2)  || tsline c_ti_fob , yaxis(1) legend(on order(1 "Gov. approval" 2 "Terms-of-trade index"))

*Figure 4
twoway tsline gob_aprob, yaxis(2)  || tsline BCOMIndex , yaxis(1) legend(on order(1 "Gov. approval" 2 "Com. index (total)"))

*Figure 5
twoway tsline gob_aprob, yaxis(2)  || tsline BCOMENIndex , yaxis(1) legend(on order(1 "Gov. approval" 2 "Com. index energy"))

*Figure 6

twoway tsline gob_aprob, yaxis(2)  || tsline BCOMGRIndex , yaxis(1) legend(on order(1 "Gov. approval" 2 "Com. index grains"))

*Figure 7
twoway tsline gob_aprob, yaxis(2)  || tsline BCOMINIndex , yaxis(1) legend(on order(1 "Gov. approval" 2 "Com. index industrial metals"))


*Figure 8
twoway tsline gob_aprob, yaxis(2) || tsline c_gtot_hab , yaxis(1) legend(on order(1 "Gov. approval" 2 "Per capita social spending"))

restore

*Figure 9

clear 
use "data_latinobarometro_2009.dta"

sort gob_aprob_crisis
graph hbar gob_aprob_crisis, over(country, sort(1) descending )  blabel(total) 
